package com.easymall.web;

import com.easymall.domain.User;
import com.easymall.service.UserService;
import com.easymall.util.BaseFactory;
import com.easymall.util.FormValidation;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RegistServlet extends javax.servlet.http.HttpServlet {

    private UserService userService;

    @Override
    public void init() throws ServletException {
        try {
            userService = BaseFactory.getFactory().getInstance(UserService.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        // 1. 获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String nickname = request.getParameter("nickname");
        String email = request.getParameter("email");
        String valistr = request.getParameter("valistr");

        // 2. 表单验证
        Map<String, List<String>> errorMap = new HashMap<>(5);
        Map<String, List<String>> tempMap = null;

        // 用户名验证
        tempMap = FormValidation.validationUsername("username", username);
        errorMap.putAll(tempMap);

        // 密码验证
        tempMap = FormValidation.validationPassword("password", password, password2);
        errorMap.putAll(tempMap);

        // 昵称验证
        tempMap = FormValidation.validationNickname("nickname", nickname);
        errorMap.putAll(tempMap);

        // 邮箱验证
        tempMap = FormValidation.validationEmail("email", email);
        errorMap.putAll(tempMap);

        // 验证验证码
        tempMap = FormValidation.validationCode("valistr", valistr);
        errorMap.putAll(tempMap);

        if (errorMap.size() == 0 ) {
            // 3. 执行业务逻辑
            List<String> errorList = new ArrayList<String>(1);
            try {
                // 判断用户名是否注册
                boolean result = userService.hasUsername(username);
                if (result) {
                    errorList.add("用户名已经注册！");

                    errorMap.put("username", errorList);
                } else {
                    // 注册用户
                    User user = new User();
                    user.setUsername(username);
                    user.setPassword(password);
                    user.setNickname(nickname);
                    user.setEmail(email);

                    result = userService.register(user);
                    if (!result) {
                        errorList.add("注册失败！");
                    } else {
                        HttpSession session = request.getSession();
                        session.setAttribute("user", user);
                    }
                }

            } catch (SQLException e) {
                e.printStackTrace();
                errorList.add("注册失败，请稍候重试！");
            }
            if (errorList.size() != 0) {
                errorMap.put("all", errorList);
            }
        }

        // 4. 响应
        if (errorMap.size() != 0) {
            request.setAttribute("errorMsg", errorMap);
            doGet(request, response);
        } else {
            response.sendRedirect("/index.jsp");
        }
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.getRequestDispatcher("/regist.jsp").forward(request, response);
    }
}
